﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapControl.Misc;
using MapControl.Misc.DAL;

namespace GPSMap.Testing
{
    public partial class frmConcatData : Form
    {
        private string file1;
        private string file2;
        private string connectString1;
        private string connectString2;
        public frmConcatData()
        {
            InitializeComponent();
        }

        private void btnOpenFile1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog obj = new OpenFileDialog();
                obj.Multiselect = false;
                obj.ShowDialog();
                if (obj.FileName != "")
                {
                    file1 = obj.FileName;
                    txtFile1Path.Text = obj.FileName;
                    connectString1 = string.Format("DataSource={0}; Password=''", obj.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOpenFile2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog obj = new OpenFileDialog();
                obj.Multiselect = false;
                obj.ShowDialog();
                if (obj.FileName != "")
                {
                    file2 = obj.FileName;
                    txtFile2Path.Text = obj.FileName;
                    connectString2 = string.Format("DataSource={0}; Password=''", obj.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private BindingSource bsGPSData;
        /// <summary>
        /// Nối dữ liệu 2 sau dữ liệu 1
        /// </summary>
        public void ConcatData1()
        {
            GPSDataRepository.ConnectionString = connectString2;
            DataTable db2 = GPSDataRepository.LoadDatabase(0, 0);

            GPSDataRepository.ConnectionString = connectString1;
            foreach (DataRow dr in db2.Rows)
            {
                GPSData obj = new GPSData(dr);
                GPSDataRepository.Save(obj);
            }

            DataTable db1 = GPSDataRepository.LoadDatabase(0, 0);
            bsGPSData = new BindingSource();
            bsGPSData.DataSource = db1.DefaultView;
            dataGridViewGPS.DataSource = bsGPSData;
            bindingNavigator1.BindingSource = bsGPSData;
            FormatDataGridView();
        }

        /// <summary>
        /// Nối dữ liệu 2 xen kẻ dữ liệu 1
        /// </summary>
        public void ConcatData2()
        {
            GPSDataRepository.ConnectionString = connectString2;
            DataTable db2 = GPSDataRepository.LoadDatabase(0, 0);

            GPSDataRepository.ConnectionString = connectString1;
            DataTable db1 = GPSDataRepository.LoadDatabase(0, 0);
            //Xóa hết dữ liệu 1
            GPSDataRepository.DeleteAll();

            int minLen = Math.Min(db2.Rows.Count, db1.Rows.Count);
            for (int i = 0; i < minLen; i++)
            {
                DataRow dr1 = db1.Rows[i];
                DataRow dr2 = db2.Rows[i];
                GPSData obj1 = new GPSData(dr1);
                GPSData obj2 = new GPSData(dr2);
                GPSDataRepository.Save(obj1);
                GPSDataRepository.Save(obj2);
            }
            
            //Add remain data
            if (db2.Rows.Count > minLen)
            {
                for (int i = minLen; i < db2.Rows.Count; i++)
                {
                    DataRow dr = db2.Rows[i];
                    GPSData obj = new GPSData(dr);
                    GPSDataRepository.Save(obj);
                }
            }
            else
            {
                for (int i = minLen; i < db1.Rows.Count; i++)
                {
                    DataRow dr = db1.Rows[i];
                    GPSData obj = new GPSData(dr);
                    GPSDataRepository.Save(obj);
                }
            }
           

            db1 = GPSDataRepository.LoadDatabase(0, 0);
            bsGPSData = new BindingSource();
            bsGPSData.DataSource = db1.DefaultView;
            dataGridViewGPS.DataSource = bsGPSData;
            bindingNavigator1.BindingSource = bsGPSData;
            FormatDataGridView();
        }

        ///// <summary>
        ///// Cập nhật Datasource từ CSDL
        ///// </summary>
        //public void LoadDataGridView()
        //{
        //    GPSDataRepository.ConnectionString = connectString2;
        //    DataTable db2 = GPSDataRepository.LoadDatabase(0, 0);

        //    GPSDataRepository.ConnectionString = connectString1;
        //    foreach (DataRow dr in db2.Rows)
        //    {
        //        GPSData obj = new GPSData(dr);
        //        GPSDataRepository.Save(obj);
        //    }

        //    DataTable db1 = GPSDataRepository.LoadDatabase(0, 0);
        //    bsGPSData = new BindingSource();
        //    bsGPSData.DataSource = db1.DefaultView;
        //    dataGridViewGPS.DataSource = bsGPSData;
        //    bindingNavigator1.BindingSource = bsGPSData;
        //    FormatDataGridView();
        //}

        public void FormatDataGridView()
        {
            dataGridViewGPS.Columns["airport_id"].Visible = false;

            dataGridViewGPS.Columns["id"].HeaderText = "Stt";
            dataGridViewGPS.Columns["name"].HeaderText = "Tên";
            dataGridViewGPS.Columns["lat"].HeaderText = "Vĩ độ";
            dataGridViewGPS.Columns["lon"].HeaderText = "Kinh độ";
            dataGridViewGPS.Columns["alt"].HeaderText = "Độ cao";
            dataGridViewGPS.Columns["alt"].ToolTipText = "Độ cao (m)";
            dataGridViewGPS.Columns["speed"].HeaderText = "Tốc độ";
            dataGridViewGPS.Columns["speed"].ToolTipText = "Tốc độ (km/h)";
            dataGridViewGPS.Columns["angle"].HeaderText = "Hướng";
            dataGridViewGPS.Columns["angle"].ToolTipText = "Hướng so với bắc bán cầu";
            dataGridViewGPS.Columns["distance"].HeaderText = "Cự ly";
            dataGridViewGPS.Columns["distance"].ToolTipText = "Khoảng cách tính từ sân bay (km)";
            dataGridViewGPS.Columns["time"].HeaderText = "Th.gian";
            dataGridViewGPS.Columns["time"].ToolTipText = "Thời gian";
            dataGridViewGPS.Columns["satnum"].HeaderText = "Vệ tinh";
            dataGridViewGPS.Columns["satnum"].ToolTipText = "Số vệ tinh đang dùng";

            dataGridViewGPS.Columns["id"].Width = 35;
            dataGridViewGPS.Columns["name"].Width = 35;
            dataGridViewGPS.Columns["name"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridViewGPS.Columns["lat"].Width = 55;
            dataGridViewGPS.Columns["lat"].DefaultCellStyle.Format = "#.00000";
            dataGridViewGPS.Columns["lon"].Width = 60;
            dataGridViewGPS.Columns["lon"].DefaultCellStyle.Format = "#.00000";
            dataGridViewGPS.Columns["alt"].Width = 48;
            dataGridViewGPS.Columns["alt"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewGPS.Columns["speed"].Width = 49;
            dataGridViewGPS.Columns["speed"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewGPS.Columns["angle"].Width = 41;
            dataGridViewGPS.Columns["angle"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewGPS.Columns["distance"].Width = 41;
            dataGridViewGPS.Columns["distance"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewGPS.Columns["time"].Width = 50;
            dataGridViewGPS.Columns["time"].DefaultCellStyle.Format = "HH:mm:ss";
            dataGridViewGPS.Columns["satnum"].Width = 50;
            dataGridViewGPS.Columns["satnum"].DefaultCellStyle.Format = "0#";
            dataGridViewGPS.Columns["satnum"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

        }

        private void btnConcat_Click(object sender, EventArgs e)
        {
            ConcatData1();
        }

        private void btnConcat2_Click(object sender, EventArgs e)
        {
            ConcatData2();
        }
    }
}
